import { RbacPermissionType } from '@/data/auth/permission';
import { PermissionTypeEnum, PermissionTypeValueEnum } from '@/enums/auth';
import { ProFormSelect, ProFormText, StepsForm } from '@ant-design/pro-components';
import { Modal } from 'antd';
import React from 'react';

export type FormValueType = {
  target?: string;
  template?: string;
  type?: string;
  time?: string;
  frequency?: string;
} & Partial<RbacPermissionType>;

export type UpdateFormProps = {
  onCancel: (flag?: boolean, formVals?: FormValueType) => void;
  onSubmit: (values: FormValueType) => Promise<void>;
  updateModalVisible: boolean;
  values: Partial<RbacPermissionType>;
};

const UpdateModal: React.FC<UpdateFormProps> = (props) => {
  return (
    <StepsForm
      stepsProps={{
        size: 'small',
      }}
      stepsFormRender={(dom, submitter) => {
        return (
          <Modal
            width={640}
            style={{
              padding: '32px 40px 48px',
            }}
            destroyOnHidden
            title="更新"
            open={props.updateModalVisible}
            footer={submitter}
            onCancel={() => {
              props.onCancel();
            }}
          >
            {dom}
          </Modal>
        );
      }}
      onFinish={props.onSubmit}
    >
      <StepsForm.StepForm
        initialValues={{
          remark: props.values.remark,
          urlPerm: props.values.urlPerm,
          type: props.values.type,
        }}
        title="基本信息"
      >
        <ProFormSelect
          rules={[
            {
              required: true,
              message: '权限类型为必填项',
            },
          ]}
          width="md"
          name="type"
          valueEnum={PermissionTypeValueEnum}
          label={'权限类型'}
          placeholder={'请选择权限类型'}
        />
        <ProFormText
          rules={[
            {
              required: true,
              message: '备注为必填项',
            },
          ]}
          width="md"
          name="remark"
          label={'备注'}
          placeholder={'请输入备注'}
        />
        <ProFormText
          rules={[
            {
              required: true,
              message: '权限标识为必填项',
            },
            ({ getFieldValue }) => ({
              validator(_, value) {
                const type = getFieldValue('type');
                // 根据不同类型定义不同的正则表达式
                let regExp;

                switch (type) {
                  case PermissionTypeEnum.Api:
                    // 格式: /auth/rbacUser/page (不含后缀)
                    regExp = /^\/[a-zA-Z0-9]+(\/[a-zA-Z0-9]+)+$/;
                    break;
                  case PermissionTypeEnum.Admin:
                    // 格式: /auth/rbacUser/page.admin (含.admin后缀)
                    regExp =/^\/[a-zA-Z0-9]+(\/[a-zA-Z0-9]+)+\.admin$/;
                    break;
                  case PermissionTypeEnum.Open:
                    // 格式: /auth/rbacUser/page.open (含.open后缀)
                    regExp = /^\/[a-zA-Z0-9]+(\/[a-zA-Z0-9]+)+\.open$/;
                    break;
                  default:
                    return Promise.resolve();
                }

                if (regExp.test(value)) {
                  return Promise.resolve();
                }

                // 根据类型返回对应的错误提示
                const messages = {
                  '0': '权限标识格式应为 /xxx/xxx/xxx',
                  '1': '权限标识格式应为 /xxx/xxx/xxx.admin',
                  '2': '权限标识格式应为 /xxx/xxx/xxx.open',
                };

                return Promise.reject(new Error(messages[type] || '权限标识格式不正确'));
              },
            }),
          ]}
          width="md"
          name="urlPerm"
          label={'权限标识'}
          placeholder={'请输入权限标识'}
        />
      </StepsForm.StepForm>
    </StepsForm>
  );
};

export default UpdateModal;
